package com.mtsmda.java.xml.jaxb;

import java.io.File;
import java.util.Iterator;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import com.mtsmda.Country;
import com.mtsmda.State;

public class JaxbXMLToJava {

	public static void main(String[] args) {
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(Country.class);
			Unmarshaller createUnmarshaller = jaxbContext.createUnmarshaller();

			File fileXML = new File("countries.xml");

			Object unmarshal = createUnmarshaller.unmarshal(fileXML);
			if (unmarshal instanceof Country) {
				Country country = (Country) unmarshal;
				System.out.println("CountryName - " + country.getCountryName());
				System.out.println("CountryPopulation - "
						+ country.getCountryPopulation());
				Iterator<State> iterator = country.getStates().iterator();
				while (iterator.hasNext()) {
					State next = iterator.next();
					System.out.println("\t" + next.getStateName() + " _ "
							+ next.getStatePopulation());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}